<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>~설문설문~</title>
<link href="css/form.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://apis.daum.net/maps/maps2.js?apikey=fdedf555ead724e65234ad4eb1ccb6760c6a6f1a"></script>
<script type="text/javascript" src="./js/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="./js/json2.js"></script>
<script type="text/javascript" src="./js/util.js"></script>
<script type="text/javascript">
	function isEmptyField(field) {
		return field.value == "";
	}
	function generateRandMac() {
		var macCodes = [];
		for (var i = 0; i < 12; i++)
			macCodes[i] = Math.floor(Math.random() * 16).toString(16).toUpperCase();
		
		var pos = 0;
		var tempMac = macCodes.join('');
		var macAddr = ""
		for (var i = 0; i < 5; i++) {
			macAddr += tempMac.substr(pos, 2) + "-";
			pos += 2;
		}
		macAddr += tempMac.substr(pos, 2);
		return macAddr;
	}

	function validate() {
		if (ap_loc.value == "" && gps_loc.value == "") {
			alert("AP나 GPS 값을 입력하세요");
			return false;
		}
		if (ap_loc_detail == "") {
			alert("AP 상세 지역명을 입력하세요");
			return false;
		}
		if (gps_loc_detail == "") {
			alert("GPS 상세 지역명을 입력하세요");
			return false;
		}
		if (ap_loc.value != "" && mac.value == "") {
			alert("MAC ADDRESS를 입력하세요");
			mac.focus();
			return false;
		}
		
		if (gps_loc.value != "" && radius.value == "") {
			alert("반경을 입력하세요");
			radius.focus();
			return false;
		}
		
		if (isNaN(radius.value)) {
			alert("반경은 숫자여야만 합니다");
			radius.focus();
			return false;
		}
		return true;
	}
	
	$('document').ready(function() {
		$('#select_ap_loc').focus();
		$('#select_ap_loc').click(function(evt) {
			openPopup("select_location.php?form_name=reg_location&addr_field_name=ap_loc", "select_loc", 320, 240);
		});
		$('#select_gps_loc').click(function(evt) {
			openPopup("select_gps_location.php", "select_gps_loc", 800, 650);
		});
		$('#gen_rand_mac').click(function() {
			mac.value = generateRandMac();
		});
		$('#submit').click(function() {
			if (validate() == false)
				return false;
			
			var formData = {
				'ap': {
					'location': trim(ap_loc.value) + (ap_loc_detail.value != "" ? " " : "") + ap_loc_detail.value,
					'mac': mac.value
				},
				'gps': {
					'location': gps_loc.value + (gps_loc_detail.value != "" ? " " : "") + gps_loc_detail.value,
					'latitude': latitude.value,
					'longitude': longitude.value,
					'radius': radius.value
				}
			};
			$.postJSON('post_location.php', formData, function(data) {
				switch (data.status) {
					case 0:
						alert('성공적으로 등록되었습니다');
						$(':text').val("");
						//window.close();
						break;
					case 1:
						alert('같은 ap주소가 이미 존재합니다\n다시 입력하여 주세요');
						break;
					case 2:
						alert('같은 mac주소가 이미 존재합니다\n다시 입력하여 주세요');
						break;
					case 3:
						alert('같은 gps주소가 이미 존재합니다\n세부 주소를 입력하여 주세요');
						break;
				}
			});
			
		});
	});
</script>
</head>
<body>
	<form id="reg_location" action="" method="post">
	<h1>표준지역명테이블 등록</h1>
		<fieldset>
			<legend>AP 등록</legend>
			<div>
				<label for="ap_loc" class="label">지역명</label>
				<input class="readonly" readonly="true" type="text" id="ap_loc" name="ap_loc" size="50" />
				<input id="select_ap_loc" type="button" value="찾기" />
			</div>
			<div>
				<label for="ap_loc_detail" class="label">상세 지역명</label>
				<input type="text" id="ap_loc_detail" name="ap_loc_detail" size="50" />
			</div>
			<div>
				<label for="mac" class="label">MAC ADDRESS</label>
				<input type="text" id="mac" name="mac" size="40" />
				<input id="gen_rand_mac" type="button" value="랜덤값 생성" />
			</div>
		</fieldset>
		<fieldset>
			<legend>GPS 등록</legend>
			<div>
				<label for="gps_loc" class="label">지역명</label>
				<input class="readonly" readonly="true" type="text" id="gps_loc" name="gps_loc" size="50" />
				<input type="button" id="select_gps_loc" value="찾기" />
			</div>
			<div>
				<label for="gps_loc_detail" class="label">상세 지역명</label>
				<input type="text" id="gps_loc_detail" name="gps_loc_detail" size="50" />
			</div>
			<div>
				<label for="latitude" class="label">위도</label>
				<input class="readonly" readonly="true" type="text" id="latitude" name="latitude" size="20" />
			</div>
			<div>
				<label for="longitude" class="label">경도</label>
				<input class="readonly" readonly="true" type="text" id="longitude" name="longitude" size="20" />
			</div>
			<div>
				<label for="radius" class="label">반경</label>
				<input type="text" id="radius" name="radius" size="20" />
				<span>m</span>
			</div>
		</fieldset>
		<fieldset>
			<div class="indent">
				<input type="button" name="submit" id="submit" value="등록" />
				<input type="button" value="창닫기" onclick="closeWindow(true)" />
				<input type="reset" value="초기화" />
			</div>
		</fieldset>
	</form>
</body>
</html>
